import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

file_centralization = "../../data/128server_result_(T100N100)_centralization.xlsx"
file_distribution = "../../data/128server_result_(T100N100)_distribution.xlsx"
file_no_control = "../../data/128server_result_(T100N100)_no_control.xlsx"
data_centralization = pd.read_excel(file_centralization, index_col=0)
data_distribution = pd.read_excel(file_distribution, index_col=0)
data_no_control = pd.read_excel(file_no_control, index_col=0)

data_centralization_t = data_centralization.transpose()
data_distribution_t = data_distribution.transpose()
data_no_control_t = data_no_control.transpose()

data_centralization_t = data_centralization_t.drop("result")
data_distribution_t = data_distribution_t.drop("result")

data_centralization_t = data_centralization_t.rename(columns={
    "App1": "App1(1 VNF)",
    "App2": "App2(1 1+1 VNF)",
    "App3": "App3(1 3way VNF)",
    "App4": "App4(2 VNF)",
    "App5": "App5(2 1+1 VNF)",
    "App6": "App6(2 3way VNF)",
    "App7": "App7(3 VNF)",
    "App8": "App8(3 1+1 VNF)",
    "App9": "App9(3 3way VNF)",
    "total": "整网可用度(平均）"
})

data_distribution_t = data_distribution_t.rename(columns={
    "App1": "App1(1 VNF)",
    "App2": "App2(1 1+1 VNF)",
    "App3": "App3(1 3way VNF)",
    "App4": "App4(2 VNF)",
    "App5": "App5(2 1+1 VNF)",
    "App6": "App6(2 3way VNF)",
    "App7": "App7(3 VNF)",
    "App8": "App8(3 1+1 VNF)",
    "App9": "App9(3 3way VNF)",
    "total": "整网可用度(平均）"
})

ax = data_centralization_t.plot()
ax.set_xlabel("第i次仿真")
ax.set_ylabel("availability")
ax.set_title("集中式控制平面业务可用度折线图")
fig = ax.get_figure()
plt.ylim([0.9996, 0.99999])
plt.legend(loc=4)
fig.savefig("../../data/availability_graph_centralization")

ax = data_distribution_t.plot()
ax.set_xlabel("第i次仿真")
ax.set_ylabel("availability")
ax.set_title("分布式控制平面业务可用度折线图")
fig = ax.get_figure()
plt.ylim([0.9996, 0.99999])
plt.legend(loc=4)
fig.savefig("../../data/availability_graph_distribution")

ax = data_no_control_t.plot()
ax.set_xlabel("第i次仿真")
ax.set_ylabel("availability")
ax.set_title("无制平面业务可用度折线图")
fig = ax.get_figure()
plt.ylim([0.9996, 0.99999])
plt.legend(loc=4)
fig.savefig("../../data/availability_graph_no_control")

ax = data_centralization_t.plot.box()
ax.set_xlabel("第i次仿真")
ax.set_ylabel("availability")
ax.set_title("集中式控制平面业务可用度箱线图")
fig = ax.get_figure()
plt.xticks(rotation=-15)
plt.ylim([0.9996, 0.99999])
plt.grid()
fig.savefig("../../data/box_graph_centralization.png", dpi=100)

ax = data_distribution_t.plot.box()
ax.set_xlabel("第i次仿真")
ax.set_ylabel("availability")
ax.set_title("分布式控制平面业务可用度箱线图")
fig = ax.get_figure()
plt.xticks(rotation=-15)
plt.ylim([0.9996, 0.99999])
plt.grid()
fig.savefig("../../data/box_graph_distribution.png", dpi=100)

ax = data_no_control_t.plot.box()
ax.set_xlabel("第i次仿真")
ax.set_ylabel("availability")
ax.set_title("无控制平面业务可用度箱线图")
fig = ax.get_figure()
plt.xticks(rotation=-15)
plt.ylim([0.9996, 0.99999])
plt.grid()
fig.savefig("../../data/box_graph_no_control.png", dpi=100)

sns.set()

fig = plt.figure(figsize=(16, 16))
for i in range(9):
    ax = fig.add_subplot(3, 3, i + 1)
    data = data_centralization_t.iloc[:, i].to_numpy()
    sns.distplot(data, norm_hist=True)
    # plt.xlim([0.9997, 0.99999])
    y_labels = ax.get_yticks()
    y_labels = y_labels / 100000
    ax.set_yticklabels(y_labels)
    ax.get_xaxis().get_major_formatter().set_scientific(False)
    ax.set_xlabel("availability")
    ax.set_ylabel("probability")
    ax.set_title("App%d \n mean:%s \n variance%s" % (i + 1, np.mean(data), np.var(data)))
fig.savefig("../../data/hist_kde_graph_centralization.png", dpi=200)

fig = plt.figure(figsize=(16, 16))
for i in range(9):
    ax = fig.add_subplot(3, 3, i + 1)
    data = data_distribution_t.iloc[:, i].to_numpy()
    sns.distplot(data, norm_hist=True)
    # plt.xlim([0.9997, 0.99999])
    y_labels = ax.get_yticks()
    y_labels = y_labels / 100000
    ax.set_yticklabels(y_labels)
    ax.get_xaxis().get_major_formatter().set_scientific(False)
    ax.set_xlabel("availability")
    ax.set_ylabel("probability")
    ax.set_title("App%d \n mean:%s \n variance%s" % (i + 1, np.mean(data), np.var(data)))
fig.savefig("../../data/hist_kde_graph_distribution.png", dpi=200)

fig = plt.figure(figsize=(16, 16))
for i in range(9):
    ax = fig.add_subplot(3, 3, i + 1)
    data = data_no_control_t.iloc[:, i].to_numpy()
    sns.distplot(data, norm_hist=True)
    # plt.xlim([0.9997, 0.99999])
    y_labels = ax.get_yticks()
    y_labels = y_labels / 100000
    ax.set_yticklabels(y_labels)
    ax.get_xaxis().get_major_formatter().set_scientific(False)
    ax.set_xlabel("availability")
    ax.set_ylabel("probability")
    ax.set_title("App%d \n mean:%s \n variance%s" % (i + 1, np.mean(data), np.var(data)))
fig.savefig("../../data/hist_kde_graph_no_control.png", dpi=200)


def remove_filers_with_boxplot(data_p):
    p = data_p.boxplot(return_type='dict')
    for index, value in enumerate(data_p.columns):
        # 获取异常值
        fliers_value_list = p['fliers'][index].get_ydata()
        # 删除异常值
        for flier in fliers_value_list:
            data_p = data_p[data_p.loc[:, value] != flier]
    return data_p


data_centralization_t = remove_filers_with_boxplot(data_centralization_t)
data_distribution_t = remove_filers_with_boxplot(data_distribution_t)
res = pd.DataFrame()
res['集中式控制面可用度均值'] = data_centralization_t.apply(np.mean)
res['分布式控制面可用度均值'] = data_distribution_t.apply(np.mean)
res['集中式控制面可用度标准差'] = data_centralization_t.apply(np.std)
res['分布式控制面可用度标准差'] = data_distribution_t.apply(np.std)
res.to_excel("../../data/res.xlsx")
